/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.common.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.page.PageBean;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.common.BaseService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.utils.Utils;

/**
 *@param <T>
 * @ClassName: BaseServiceImpl
 *@Description: 
 *@author youjun
 *@date 2017年6月23日 上午9:37:13
 */
public class BaseServiceImpl<T extends Serializable> implements BaseService<T> {

	/**
	 *@Title: getUserId
	 *@Description: 从map中获得当前登录用户的ID
	 *@return Integer
	 *@author youjun
	 *@date 2017年6月9日 上午11:43:26
	 *@param map
	 *@return
	 */
	protected Integer getUserId(final Map<String, Object> map) {
		final Object userId = map.get(Cons.MAP_KEY_USER_ID);
		return userId==null?null:(Integer) userId;
	}
	
	/**
	 *@Title: convertPageBean
	 *@Description: 
	 *@return PageBean<T>
	 *@author youjun
	 *@date 2017年7月6日 下午2:50:34
	 *@param dataList
	 *@param total
	 *@param pageBean
	 *@return
	 */
	public PageBean<T> convertPageBean(final List<T> dataList, final int total, final PageBean<T> pageBean) {

		final int pageSize = pageBean.getPageSize();
		int pages = 0;
		pageBean.setTotal(total);
		pageBean.setDataList(dataList);
		
		if (total > 0) {
			if (total%pageSize==0) {
				pages = total/pageSize;
			} else {
				pages = total/pageSize + 1;
			}
		}
		pageBean.setPages(pages);
		return pageBean;
	}
	
	/**
	 *@Title: validateBasicParams
	 *@Description: 验证公共参数,<b>企业code、用户ID不能为空,非该逻辑请调用其他</b>
	 *@return Response
	 *@author youjun
	 *@date 2017年6月27日 上午11:10:14
	 *@param request
	 */
	protected Response validateBasicParams(final Request request) {
		if(Utils.isEmpty(request)) {
			return new Response(ResponseEnum.PARAM_NULL);
		}
		if(Utils.isEmpty(request.getEnterpriseCode())){
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "企业code不能为空");
		}
		if(Utils.isEmpty(request.getUserId())){
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "用户ID不能为空");
		}
		return new Response(ResponseEnum.SUCCESS);
	}
	
	/**
	 *@Title: validateBasicParams
	 *@Description: 验证企业code和
	 *@return Response
	 *@author youjun
	 *@date 2017年7月15日 下午4:50:49
	 *@param request
	 *@return
	 */
	protected Response validateBasicParams(final Request request, final boolean isValidTerminalId, 
			final boolean isValidUserId) {
		if(request==null) {
			return new Response(ResponseEnum.PARAM_NULL);
		}
		if(StringUtils.isBlank(request.getEnterpriseCode())) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "企业code不能为空");
		}
		if(isValidUserId && request.getUserId()==null) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "用户ID不能为空");
		}
		return new Response(ResponseEnum.SUCCESS);
	}
	
	/**
	 *@Title: validateBasicParamsThrow
	 *@Description: 验证基础参数,验证不通过抛出异常
	 *@return void
	 *@author youjun
	 *@date 2017年8月4日 下午5:31:21
	 *@param request
	 *@param isValidTerminalId
	 *@param isValidUserId
	 *@throws ServiceException
	 */
	protected void validateBasicParamsThrow(final Request request, final boolean isValidTerminalId, 
			final boolean isValidUserId) throws ServiceException {
		
		if(request==null) {
			throw new ServiceException("参数不能为空");
		}
		if(StringUtils.isBlank(request.getEnterpriseCode())) {
			throw new ServiceException("企业code不能为空");
		}
		if(isValidUserId && request.getUserId()==null) {
			throw new ServiceException("终端用户ID不能为空");
		}
	}
	
	
	/**
	 *@Title: fillBaseRequestInfo
	 *@Description: 填充基础数据:企业code、用户ID
	 *@return void
	 *@author youjun
	 *@date 2017年8月4日 下午4:35:46
	 *@param sourceRequest
	 *@param targetRequest
	 *@throws ServiceException
	 */
	protected void fillBaseRequestInfo(final Request sourceRequest, final Request targetRequest) throws ServiceException {
		if(sourceRequest==null
				||targetRequest==null) {
			throw new ServiceException("参数不能为空");
		}
		targetRequest.setEnterpriseCode(sourceRequest.getEnterpriseCode());
		targetRequest.setUserId(sourceRequest.getUserId());
	}
}
